// pages/person/person.js

const httpUtils = require('../../utils/httpUtils')
const ui = require('../../utils/ui')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    headerUrl: "/images/person/defaultHeader.png",
    phone: "登录/注册",
    guanzhuNum: 0,
    fensiNum: 0,
    amountNum: 0,
    coinNum: 0,
    doingdaitijiao: 0,
    doingdaishenhe: 0,
    doingyijujue: 0,
    doingyihuoshang: 0,
    doingshibai: 0,
    puhlishdaishenhe: 0,
    puhlishdaifukuan: 0,
    puhlishjinxingzhong: 0,
    puhlishdengdaijieshu: 0,
    puhlishyijieshu: 0,
    puhlishshenheshibai: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let obj = {
      method: 'GET',
      url: '/user/getWechatToken'
    }
    httpUtils.request(obj).then(res => {
    }).catch(err => {
      console.log('ERROR')
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
    if (wx.getStorageSync('USERDATA')) {
      this.requestUserInfo();
      this.requestTaskInfo();
    } else {
      //清除数据
      this.setData({
        headerUrl: "/images/person/defaultHeader.png",
        phone: "登录/注册",
        guanzhuNum: 0,
        fensiNum: 0,
        amountNum: 0,
        coinNum: 0,
        doingdaitijiao: 0,
        doingdaishenhe: 0,
        doingyijujue: 0,
        doingyihuoshang: 0,
        doingshibai: 0,
        puhlishdaishenhe: 0,
        puhlishdaifukuan: 0,
        puhlishjinxingzhong: 0,
        puhlishdengdaijieshu: 0,
        puhlishyijieshu: 0,
        puhlishshenheshibai: 0
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  //判断用户是否登录,未登录弹出登录框
  userLoginStatus() {
    if (wx.getStorageSync('USERDATA')) {
      return true;
    } else {
      wx.showModal({
        title: '提示',
        content: '需要登录后体验其他功能',
        complete: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
        }
      });
      return false;
    }
  },
  //点击顶部
  headerViewTap() {
    if (this.userLoginStatus()) {

    }
  },
  //请求个人信息
  requestUserInfo() {
    let obj = {
      method: 'GET',
      url: '/user/' + wx.getStorageSync('USERDATA').platformId
    }
    httpUtils.request(obj).then(res => {
      this.setData({
        headerUrl: res.data.avatar,
        phone: res.data.nickName,
        guanzhuNum: res.data.attentionCount,
        fensiNum: res.data.fansCount,
        amountNum: res.data.balanceAmount,
        coinNum: res.data.balanceCoinAmount
      })
      ui.showToast(res.data.error_message)
    }).catch(err => {
      console.log('ERROR')
    });
  },
  //请求任务个数
  requestTaskInfo() {
    let obj = {
      method: 'GET',
      url: '/user/' + wx.getStorageSync('USERDATA').platformId + '/task-info'
    }
    httpUtils.request(obj).then(res => {
      this.setData({
        doingdaitijiao: res.data.myOrderReadyToSubmitOrderCount,
        doingdaishenhe: res.data.myOrderReadyToApproveCount,
        doingyijujue: res.data.myOrderRejectCount,
        doingyihuoshang: res.data.myOrderGotMoneyCount,
        doingshibai: res.data.myOrderFailedCount,
        puhlishdaishenhe: res.data.taskReadyToSubmitOrderCount,
        puhlishdaifukuan: res.data.taskReadyToPayCount,
        puhlishjinxingzhong: res.data.taskInprocessCount,
        puhlishdengdaijieshu: res.data.taskWaitToEndCount,
        puhlishyijieshu: res.data.taskCompleteCount,
        puhlishshenheshibai: res.data.taskRejectCount
      })
      ui.showToast(res.data.error_message)
    }).catch(err => {
      console.log('ERROR')
    });
  },
  //点击设置
  settingPress() {
    wx.navigateTo({
      url: '/pages/setting/setting',
    })
  },
  //头像加载错误处理
  headerImageLoadFail() {
    this.setData({
      headerUrl: "/images/person/defaultHeader.png"
    })
  },
  //进入提现页面
  actionWithdrawCash() {
    if (this.userLoginStatus()) {
      wx.navigateTo({
        url: '/pages/person/withdrawCash/withdrawCash',
      })
    }
  },
  //进入提现明细页面
  actionWithdrawRecord() {
    if (this.userLoginStatus()) {
      wx.navigateTo({
        url: '/pages/person/withdrawRecord/withdrawRecord',
      })
    }
  },
  //进入金币出售页面
  actionCoinSale() {
    if (this.userLoginStatus()) {
      wx.navigateTo({
        url: '/pages/person/coin/saleCoin',
      })
    }
  },
  //进入金币明细
  actionCoinDetail() {
    if (this.userLoginStatus()) {
      wx.navigateTo({
        url: '/pages/person/coin/coinDetail',
      })
    }
  },
  //联系客服
  actionKefu() {
    wx.openCustomerServiceChat({
      corpId: 'ww1a67d4324104daaa',
      extInfo: {
        url: 'https://work.weixin.qq.com/kfid/kfc967f9513998cc218',
      },
    })
  },
  //点击进入我在做的任务
  actionDoingTask(event) {
    if (this.userLoginStatus()) {
      wx.navigateTo({
        url: '/pages/doing/doingTask?index='+event.currentTarget.dataset.index,
      })
    }
  },
  //点击进入我发布的任务
  actionPublishTask(event) {
    if (this.userLoginStatus()) {
      wx.navigateTo({
        url: '/pages/publish/manageTask?index='+event.currentTarget.dataset.index,
      })
    }
  },
  //发布任务
  actionPublish() {
    if (this.userLoginStatus()) {
      wx.navigateTo({
        url: '/pages/publish/publishCategory',
      })
    }
  },
  //进入关注粉丝列表
  actionFuns() {
    wx.navigateTo({
      url: '/pages/person/funs/funs',
    })
  }
})